<template>
  <div class="app-container">
    <div style="text-align: center">{{createStatus?'更新':''}}维保信息</div>
    <div style="text-align: center;margin-bottom: 30px">
    </div>
    <div style="width: 50%;margin:0px auto;vertical-align:middle;height:100%">
      <el-form :model="maintenanceFrom" ref="maintenanceFrom" :rules="mastercardlogFromRules" label-width="100px">
        <el-form-item label="执行人员" prop="designee" >
          <el-select v-model="maintenanceFrom.designee" placeholder="请选择执行人员" style="width: 100%">
            <el-option v-for="(item,index) in designeeList" :key="index"  :value="item.ucId" :label="item.name" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="业主企业" prop="enterpriseId" >
          <el-select v-model="maintenanceFrom.enterpriseId" placeholder="请选择业主企业" style="width: 100%"  >
            <el-option v-for="(item,index) in enterpriseList" :key="index"  :value="item.ucId" :label="item.name" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维保企业" prop="manitenanceEnterpriseId" v-if="maintenanceFrom.faultWorkOrder==false">
          <el-select v-model="maintenanceFrom.manitenanceEnterpriseId" placeholder="请选择企业"  style="width: 100%" @change="getInUserList" >
            <el-option v-for="(item,index) in manitenanceEnterpriseList" :key="index"  :value="item.ucId" :label="item.name" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工单名称" prop="name">
          <el-input v-model="maintenanceFrom.name" placeholder="" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="工单类型" prop="faultWorkOrder" v-if="maintenanceFrom.message">
          <el-input v-model="maintenanceFrom.faultWorkOrder"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" style="text-align: center" class="dialog-footer">
      <el-button @click="cancelCommission">取 消</el-button>
      <el-button :loading="addLoading" type="primary" @click="updateInMaintenanceRecord">更新</el-button>
    </div>
  </div>
</template>

<script>
import {getInEnterpriseDominantList} from "../../api/enterpriseDominant";
import {
  addMaintenanceRecord,
  getInMaintenanceRecordVo,
  updateInMaintenanceRecord
} from "../../api/maintenanceRecord";
import {getEquipmentList} from "../../api/equipment";
import {getInUserList} from "../../api/enterprise";
export default {
  name: "edit",
  data(){
    return{
      maintenanceFrom:{

      },
      mastercardlogFromRules:{
        enterpriseId: [{ required: true, message: '请选择企业', trigger: 'blur' }],
        // manitenanceEnterpriseId: [{ required: true, message: '请选择维保企业', trigger: 'blur' }],
        name: [{ required: true, message: '请输入工单名称', trigger: 'blur' }],
      },
      addLoading:false,
      equipmentList:[],
      enterpriseList:[],
      manitenanceEnterpriseList:[],
      designeeList:[],
        faultWorkOrder:this.$route.params.faultWorkOrder,
    }
  },
  computed: {
    createStatus(){
      return this.$route.params.ucId=='0'
    }
  },
  created() {
    if(!this.createStatus){
      let ucId =this.$route.params.ucId
      console.log(ucId)
      this.getInMaintenanceRecordVo(ucId)
    }
    this.getInEnterpriseDominantList();

    this.getInOwnerDominantList();
  },
  methods:{
    cancelCommission(){
      window.history.back()
    },
    getInMaintenanceRecordVo(val) {
      getInMaintenanceRecordVo({ucId: val}).then(res =>{
        console.log(res)
        this.maintenanceFrom = res.data
          if (this.faultWorkOrder!=2){
              this.getInUserList(res.data.manitenanceEnterpriseId)
          }else {
              this.getInUserList(res.data.enterpriseId)
          }
      }).catch(res => {
        this.addLoading = false
        this.$notify.error({
          title: '失败',
          message: res.msg
        })
      })
    },

    updateInMaintenanceRecord() {
      this.addLoading = true
      this.$refs['maintenanceFrom'].validate(valid => {
        if (valid) {
          updateInMaintenanceRecord(this.maintenanceFrom).then(res => {
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "更新维保信息成功"
            })
            this.cancelCommission()
          }).catch(res => {
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        } else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },
    getInEnterpriseDominantList() {
      getInEnterpriseDominantList({dominantType:8,deleted:false}).then(res => {
        this.manitenanceEnterpriseList=res.data;
      }).catch(res => {
        this.addLoading = false
        this.$notify.error({
          title: '失败',
          message: res.msg
        })
      })
    },
    getInOwnerDominantList() {
        getInEnterpriseDominantList({dominantType: 7||8,deleted : false}).then(res => {
          this.enterpriseList = res.data;
        }).catch (res => {
          this.addLoading = false
          this.$notify.error({
            title: '失败',
            message: res.msg
          })
        })
    },
    getInUserList(val) {
      getInUserList({flagId:val,deleted:false}).then(res => {
        this.designeeList = res.data
      }).catch(res => {
        this.addLoading = false
        this.$notify.error({
          title: '失败',
          message: res.msg
        })
      })
    },


  }

}
</script>

<style
    scoped>

</style>